package com.cnswhy.cloud.server.system.controller;

import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.system.entity.CommonDictionary;
import com.cnswhy.cloud.server.system.dto.CommonDictionarySaveDTO;
import com.cnswhy.cloud.server.system.dto.CommonDictionaryUpdateDTO;
import com.cnswhy.cloud.server.system.dto.CommonDictionaryPageDTO;
import com.cnswhy.cloud.server.system.entity.CommonDictionaryItem;
import com.cnswhy.cloud.server.system.service.CommonDictionaryItemService;
import com.cnswhy.cloud.server.system.service.CommonDictionaryService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 字典类型
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/commonDictionary")
@Api(value = "CommonDictionary", tags = "字典类型")
//@PreAuth(replace = "commonDictionary:")
public class CommonDictionaryController extends SuperController<CommonDictionaryService, Long, CommonDictionary, CommonDictionaryPageDTO, CommonDictionarySaveDTO, CommonDictionaryUpdateDTO> {


    @Autowired
    private CommonDictionaryItemService dictionaryItemService;

    @Override
    public R<Boolean> handlerDelete(List<Long> ids) {
        this.baseService.removeByIds(ids);
        this.dictionaryItemService.remove(Wraps.<CommonDictionaryItem>lbQ().in(CommonDictionaryItem::getDictionaryId, ids));
        return this.success(true);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CommonDictionary> commonDictionaryList = list.stream().map((map) -> {
            CommonDictionary commonDictionary = CommonDictionary.builder().build();
            return commonDictionary;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(commonDictionaryList));
    }
}
